package com.fingard.dsp.bank.directbank.cib03;

import com.fingard.common.utils.json.JSONUtils;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.VerifyState;
import com.fingard.dsp.bank.directbank.bankInterface.IAuthentication;
import com.fingard.dsp.bank.reqrespobj.auth.ReqAuth;
import com.fingard.dsp.bank.reqrespobj.auth.ReqQryAuth;
import com.fingard.dsp.bank.reqrespobj.auth.RetAtsAuth;
import com.fingard.dsp.bank.reqrespobj.auth.detail.RetAtsAuthDetail;
import com.fingard.text.StringHelper;

import java.util.Date;
import java.util.HashMap;

public class CIB03Authentication extends CIB03Base implements IAuthentication {
	@Override
	public RetAtsAuth dealAuth(ReqAuth p_reqAuth) throws Exception {
		RetAtsAuth retAts = new RetAtsAuth(p_reqAuth.reqHeader);
		RetAtsAuthDetail tmpRetDetail = new RetAtsAuthDetail();
		
		try {
			String authMerId = getBankConfig().getValueAsString("authMerId");
			if (StringHelper.hasAnyChar(authMerId)) {
				ActSetItem actItem = getBankConfig().getActSetItem(authMerId, "auth");
				if (actItem != null) {
					 HashMap<String, String> postMapParams = new HashMap<String, String>();
			            postMapParams.put("version", "1.0.2");
			            postMapParams.put("mchtId", actItem.merchantId);
			            postMapParams.put("signType", "RSA");
			            postMapParams.put("serialNo", p_reqAuth.authDetail.askNumber);
			            postMapParams.put("transTime", Format.DateTime14Format.format(new Date()));
			            postMapParams.put("acctNo", p_reqAuth.authDetail.authAct);
			            String cardType = p_reqAuth.authDetail.cardType;
			            if ("1".equals(cardType)) {
			            	postMapParams.put("acctType", "5");
			            } else if ("3".equals(cardType) || "4".equals(cardType)) {
			            	postMapParams.put("acctType", "2");
			            } else {
			            	postMapParams.put("acctType", "1");
			            }
			            postMapParams.put("acctName", p_reqAuth.authDetail.authName);
			            postMapParams.put("bankName", ""); 
						postMapParams.put("bankNo", "");  // todo 
			            postMapParams.put("certType", "0");
			            postMapParams.put("certNo", p_reqAuth.authDetail.certNum);
			            postMapParams.put("rsrvPhoneNo", p_reqAuth.authDetail.cellPhone);
			            postMapParams.put("expireDate", "");
			            postMapParams.put("cvn", "");
			            postMapParams.put("sendSms", "false");
			            postMapParams.put("mac", generateMAC(postMapParams, actItem));
			            
			            String tmpStrRet = sendDataToBank(JSONUtils.map2JSON(postMapParams), "authSubmit");
			            if (StringHelper.hasAnyChar(tmpStrRet)) {
			        		if (tmpStrRet.indexOf("{") >= 0) {
			        			HashMap<String, String> tmpJsonRet = JSONUtils.json2Map(tmpStrRet);
			    				
			    				String tmpRespCode = tmpJsonRet.get("respCode");
			    				String tmpRespInfo = tmpJsonRet.get("respMsg");
			    				
			    				tmpRetDetail.respCode = "0";
			    				tmpRetDetail.respInfo = tmpRespInfo;
			    				tmpRetDetail.reqSeqID = tmpJsonRet.get("serialNo");
			    				
			    				tmpRetDetail.originalCode = tmpRespCode;
			            		tmpRetDetail.originalInfo = tmpRespInfo;
			            		tmpRetDetail.unifiedCode = getBankConfig().getMapValueByKey("AuthMapping", tmpRetDetail.originalCode);
			            		tmpRetDetail.unifiedInfo = tmpRespInfo;
			            		if ("E0000".equals(tmpRespCode)) {
				            		tmpRetDetail.verifyState = VerifyState.SUCCESS;
				            	} else {
				            		tmpRetDetail.verifyState = VerifyState.FAIL;
				            		tmpRetDetail.failType = getBankConfig().getMapValueByKey("failTypeMapping", tmpRetDetail.originalCode);
				            	}
			        		} else {
			        			tmpRetDetail.respCode = "-1";
			                	tmpRetDetail.respInfo = "银行响应数据不合法";
			        		}
			        	} else {
			        		tmpRetDetail.respCode = "-1";
			            	tmpRetDetail.respInfo = "银行响应为空";
			        	}
				} else {
					tmpRetDetail.respCode = "-1";
					tmpRetDetail.respInfo = "未找到账号:[" + authMerId + "]商户配置信息!";
				}
			} else {
				tmpRetDetail.respCode = "-1";
				tmpRetDetail.respInfo = "广银联鉴权默认商户账号:[" + authMerId + "]内容为空!";
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			tmpRetDetail.respCode = "-1";
			tmpRetDetail.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}
		
		if (tmpRetDetail.respCode.equals("-1") && StringHelper.isNullOrEmpty(tmpRetDetail.reqSeqID)) {
			tmpRetDetail.reqSeqID = p_reqAuth.authDetail.askNumber;
		}
		
		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}

	@Override
	public RetAtsAuth dealQryAuth(ReqQryAuth p_reqAuth) throws Exception {
		RetAtsAuth retAts = new RetAtsAuth(p_reqAuth.reqHeader);
		RetAtsAuthDetail tmpRetDetail = new RetAtsAuthDetail();
		tmpRetDetail.respCode = "-1";
		tmpRetDetail.respInfo = "渠道暂不支持该交易类型:" + p_reqAuth.reqHeader.transType;
		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}
}
